import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Date;

import Interfaces.IDataChannel;
import Messages.MonitorDataMassage;
import Messages.MonitorDataTypeEnum;


public class DataChannel implements IDataChannel
{
	private DatagramSocket m_socket;
	private int m_monitorPort;
	private String m_monitorIP; 
	
	public DataChannel(String monitorIP, int monitorPort) throws SocketException, UnknownHostException
	{
		m_socket = new DatagramSocket();
		m_monitorPort = monitorPort;
		m_monitorIP = monitorIP;
	}

	public void SendData(MonitorDataTypeEnum monitorDataType, Object[] params,
			Date timeStamp, String data) throws IOException 
	{
		MonitorDataMassage message = new MonitorDataMassage();
		message.Data = data;
		message.parameters = params;
		message.TimeStamp = timeStamp;
		message.Type = monitorDataType;
		
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
	    ObjectOutputStream oos = new ObjectOutputStream(baos);
	    oos.flush();
	    oos.writeObject(message);
	    oos.flush();
	    
	    // get the byte array of the object
	    byte[] buffer = baos.toByteArray();

	    DatagramPacket packet = new DatagramPacket(buffer, buffer.length, InetAddress.getByName(m_monitorIP), m_monitorPort);
	    m_socket.send(packet);
	}
}
